package com.maker.mcp.client.controller;


import com.maker.mcp.client.enums.SSEMsgType;
import com.maker.mcp.client.service.ChatService;
import com.maker.mcp.client.utils.SSEserver;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("sse")
public class SSeController {
    @Resource
    private ChatService chatService;
    /**
     * 前端发送链接的请求，连接SSE服务
     * @return
     */
    @GetMapping(path="connect",produces = {MediaType.TEXT_EVENT_STREAM_VALUE})
    public SseEmitter connect(@RequestParam String userId){

       return  SSEserver.connect(userId);
    }


    @GetMapping(path="sendMsg")
    public String sendMsg(@RequestParam String userId,@RequestParam String message){

        SSEserver.sendMsg(userId,message, SSEMsgType.MESSAGE);

        return "发送成功";
    }
    /**
     * 发送消息给所有用户
     * @param message
     * @return
     */
    @GetMapping(path="sendMsgAll")
    public String sendMsgAll(@RequestParam String message){

        SSEserver.sendMsgToAllUsers(message);

        return "发送成功";
    }

}
